<template>
  <div class="JADP-common-layout">
    <div class="JADP-common-layout-left">
      <div class="JADP-common-title">
        <h2>证件列表</h2>
      </div>
      <el-tree
        :data="treeData"
        :props="defaultProps"
        default-expand-all
        highlight-current
        ref="treeBox"
        :expand-on-click-node="false"
        @node-click="handleNodeClick"
        class="JADP-common-el-tree"
        node-key="id"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <i :class="data.icon"></i>
          <span class="text">{{ node.label }}</span>
        </span>
      </el-tree>
    </div>
    <div class="JADP-common-layout-center">
      <el-row class="JADP-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="姓名">
              <user-select
                v-model="query.papersname"
                placeholder="请选择
                                    "
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="公司">
              <comSelect
                v-model="query.paperscompany"
                placeholder="系统自动生成"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门">
              <depSelect
                v-model="query.createdept"
                placeholder="系统自动生成"
              />
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="6">
              <el-form-item label="证件类型">
                <el-select
                  v-model="query.paperstype"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in paperstypeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="证照号码">
                <el-input
                  v-model="query.papersNum"
                  placeholder="请输入"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="证照颁发机构">
                <el-input
                  v-model="query.papersdept"
                  placeholder="请输入"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="生效日期">
                <el-date-picker
                  v-model="query.starttime"
                  type="daterange"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="失效日期">
                <el-date-picker
                  v-model="query.endtime"
                  type="daterange"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否年审">
                <el-select v-model="query.nianshen" placeholder="" clearable>
                  <el-option
                    v-for="(item, index) in nianshenOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="下次年审日期">
                <el-date-picker
                  v-model="query.nextnianshen"
                  type="daterange"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="培训开始日期">
                <el-date-picker
                  v-model="query.trainstarttime"
                  type="daterange"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="培训结束日期">
                <el-date-picker
                  v-model="query.trainendtime"
                  type="daterange"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="培训机构">
                <el-input
                  v-model="query.traindept"
                  placeholder="请输入"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select v-model="query.status" placeholder="" clearable>
                  <el-option
                    v-for="(item, index) in statusOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="备注">
                <el-input
                  v-model="query.remarks"
                  placeholder="请输入"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建用户">
                <userSelect
                  v-model="query.creatoruserid"
                  placeholder="系统自动生成"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建时间">
                <el-date-picker
                  v-model="query.creatortime"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  type="daterange"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="修改时间">
                <el-date-picker
                  v-model="query.lastmodifytime"
                  value-format="timestamp"
                  format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  type="daterange"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="修改用户">
                <userSelect
                  v-model="query.lastmodifyuserid"
                  placeholder="系统自动生成"
                />
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
              >
                展开
              </el-button>
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                v-else
              >
                收起
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JADP-common-layout-main JADP-flex-main">
        <div class="JADP-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              v-has="'btn_add'"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button
              type="text"
              icon="el-icon-download"
              @click="exportData()"
              v-has="'btn_download'"
              >导出
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleBatchRemoveDel()"
              v-has="'btn_batchRemove'"
              >批量删除
            </el-button>
          </div>
          <div class="JADP-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JADP-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull :isContainer="true" />
            <!-- <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options JADP-common-head-icon"
                :underline="false"
              />
            </el-tooltip> -->
          </div>
        </div>
        <JADP-table
          v-loading="listLoading"
          :data="list"
          @sort-change="sortChange"
          has-c
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            prop="papersname"
            label="姓名"
            align="left"
            v-if="jadp.hasP('papersname')"
          />
          <el-table-column
            prop="paperscompany"
            label="公司"
            align="left"
            v-if="jadp.hasP('paperscompany')"
          />
          <el-table-column
            prop="createdept"
            label="部门"
            align="left"
            v-if="jadp.hasP('createdept')"
          />
          <el-table-column
            prop="paperstype"
            label="证件类型"
            align="left"
            v-if="jadp.hasP('paperstype')"
          />
          <el-table-column
            prop="papersNum"
            label="证照号码"
            align="left"
            v-if="jadp.hasP('papersNum')"
          />
          <el-table-column
            prop="papersdept"
            label="证照颁发机构"
            align="left"
            v-if="jadp.hasP('papersdept')"
          />
          <el-table-column
            prop="starttime"
            label="生效日期"
            align="left"
            v-if="jadp.hasP('starttime')"
          />
          <el-table-column
            prop="endtime"
            label="失效日期"
            align="left"
            v-if="jadp.hasP('endtime')"
          />
          <el-table-column
            prop="nianshen"
            label="是否年审"
            align="left"
            v-if="jadp.hasP('nianshen')"
          />
          <el-table-column
            prop="nextnianshen"
            label="下次年审日期"
            align="left"
            v-if="jadp.hasP('nextnianshen')"
          />
          <el-table-column
            prop="trainstarttime"
            label="培训开始日期"
            align="left"
            v-if="jadp.hasP('trainstarttime')"
          />
          <el-table-column
            prop="trainendtime"
            label="培训结束日期"
            align="left"
            v-if="jadp.hasP('trainendtime')"
          />
          <el-table-column
            prop="traindept"
            label="培训机构"
            align="left"
            v-if="jadp.hasP('traindept')"
          />
          <el-table-column
            prop="status"
            label="状态"
            align="left"
            v-if="jadp.hasP('status')"
          />
          <el-table-column
            prop="remarks"
            label="备注"
            align="left"
            v-if="jadp.hasP('remarks')"
          />
          <el-table-column
            prop="creatoruserid"
            label="创建用户"
            align="left"
            v-if="jadp.hasP('creatoruserid')"
          />
          <el-table-column
            prop="creatortime"
            label="创建时间"
            align="left"
            v-if="jadp.hasP('creatortime')"
          />
          <el-table-column
            prop="lastmodifytime"
            label="修改时间"
            align="left"
            v-if="jadp.hasP('lastmodifytime')"
          />
          <el-table-column
            prop="lastmodifyuserid"
            label="修改用户"
            align="left"
            v-if="jadp.hasP('lastmodifyuserid')"
          />
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id)"
                v-has="'btn_edit'"
                >编辑
              </el-button>
              <el-button
                type="text"
                class="JADP-table-delBtn"
                v-has="'btn_remove'"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </JADP-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JADP-Form v-if="formVisible" ref="JADPForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
  </div>
</template>

<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import JADPForm from "./Form";
import ExportBox from "./ExportBox";
import { previewDataInterface } from "@/api/systemData/dataInterface";

export default {
  components: { JADPForm, ExportBox },
  data() {
    return {
      showAll: false,
      query: {
        papersname: undefined,
        paperscompany: undefined,
        createdept: undefined,
        paperstype: undefined,
        papersNum: undefined,
        papersdept: undefined,
        starttime: undefined,
        endtime: undefined,
        nianshen: undefined,
        nextnianshen: undefined,
        trainstarttime: undefined,
        trainendtime: undefined,
        traindept: undefined,
        status: undefined,
        remarks: undefined,
        creatoruserid: undefined,
        creatortime: undefined,
        lastmodifytime: undefined,
        lastmodifyuserid: undefined,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "papersname", label: "姓名" },
        { prop: "paperscompany", label: "公司" },
        { prop: "createdept", label: "部门" },
        { prop: "paperstype", label: "证件类型" },
        { prop: "papersNum", label: "证照号码" },
        { prop: "papersdept", label: "证照颁发机构" },
        { prop: "starttime", label: "生效日期" },
        { prop: "endtime", label: "失效日期" },
        { prop: "nianshen", label: "是否年审" },
        { prop: "nextnianshen", label: "下次年审日期" },
        { prop: "trainstarttime", label: "培训开始日期" },
        { prop: "trainendtime", label: "培训结束日期" },
        { prop: "traindept", label: "培训机构" },
        { prop: "status", label: "状态" },
        { prop: "remarks", label: "备注" },
        { prop: "creatoruserid", label: "创建用户" },
        { prop: "creatortime", label: "创建时间" },
        { prop: "lastmodifytime", label: "修改时间" },
        { prop: "lastmodifyuserid", label: "修改用户" },
      ],
      treeData: [],
      treeActiveId: "",
      paperstypeOptions: [],
      nianshenOptions: [],
      statusOptions: [],
    };
  },
  computed: {},
  created() {
    this.getTreeView();
    this.getpaperstypeOptions();
    this.getnianshenOptions();
    this.getstatusOptions();
  },
  methods: {
    getpaperstypeOptions() {
      getDictionaryDataSelector("fdaf414ad8e0478c9f8fa6dbf15cfa3c").then(
        (res) => {
          this.paperstypeOptions = res.data.list;
        }
      );
    },
    getnianshenOptions() {
      getDictionaryDataSelector("37b6c2c29a7843aa91838601f4ffd1f2").then(
        (res) => {
          this.nianshenOptions = res.data.list;
        }
      );
    },
    getstatusOptions() {
      getDictionaryDataSelector("37b6c2c29a7843aa91838601f4ffd1f2").then(
        (res) => {
          this.statusOptions = res.data.list;
        }
      );
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    getTreeView() {
      getDictionaryDataSelector("fdaf414ad8e0478c9f8fa6dbf15cfa3c").then(
        (res) => {
          this.treeData = res.data.list;
          this.$nextTick(() => {
            this.treeActiveId = this.treeData[0].id;
            this.$refs.treeBox.setCurrentKey(this.treeActiveId);
            this.initData();
          });
        }
      );
    },
    handleNodeClick(data) {
      this.treeActiveId = data.id;
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.query.paperstype = data.id;
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        organizeId:this.$store.getters.organizeId
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      request({
        url: `/api/hse.pagers/Papers`,
        method: "get",
        data: query,
      }).then((res) => {
        this.list = res.data.list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/hse.pagers/Papers/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      const res = val.map((item) => item.id);
      this.multipleSelection = res;
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: "error",
          message: "请选择一条数据",
          duration: 1500,
        });
        return;
      }
      const ids = this.multipleSelection.join();
      this.$confirm("您确定要删除这些数据吗, 是否继续？", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/hse.pagers/Papers/batchRemove/${ids}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JADPForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query };
      request({
        url: `/api/hse.pagers/Papers/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
  },
};
</script>
